package com.lb.shopguide.adapter;

import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayout.widget.MsgView;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.UpdateInfoBean;
import com.lb.shopguide.entity.home.HomeMijiBean;
import com.lb.shopguide.entity.home.HomeMsgBean;
import com.lb.shopguide.entity.home.HomeOrderGoodsBean;
import com.lb.shopguide.entity.home.HomeOrderRobBean;
import com.lb.shopguide.entity.home.HomeOrderUpdateBean;
import com.lb.shopguide.entity.home.HomeRecommendBean;
import com.lb.shopguide.util.pic.ImageLoaderUtils;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import java.util.List;

/**
 * Created by kang on 2017/7/15.
 */

public class AdapterHomeInfo extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> implements FlexibleDividerDecoration.PaintProvider {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public AdapterHomeInfo(List<MultiItemEntity> data) {
        super(data);
        addItemType(AppConstant.HOME_ORDER_UPDATE_TYPE, R.layout.item_order_update);
        addItemType(AppConstant.HOME_ORDER_ROB_TYPE, R.layout.item_order_update);
        addItemType(AppConstant.HOME_MSG_TYPE, R.layout.item_home_msg);
        addItemType(AppConstant.HOME_MIJI_TYPE, R.layout.item_home_miji);
        addItemType(AppConstant.HOME_NEARSHOP_RECOMMEND, R.layout.item_home_recommed_activity);
        addItemType(AppConstant.HOME_SYSTEM_TYPE, R.layout.item_home_system_update);
    }

    @Override
    protected void convert(BaseViewHolder helper, final MultiItemEntity item) {
        View view = helper.itemView;
        TextView tvSubmit = view.findViewById(R.id.tv_submit);
        switch (helper.getItemViewType()) {
            case AppConstant.HOME_ORDER_UPDATE_TYPE:
            case AppConstant.HOME_ORDER_ROB_TYPE:
                TextView tvProductName = view.findViewById(R.id.tv_product_name);
                TextView tvSize = view.findViewById(R.id.tv_size);
                TextView tvPrice = view.findViewById(R.id.tv_price);
                ImageView ivProductPic = view.findViewById(R.id.iv_goods);
                TextView tvOrderFrom = view.findViewById(R.id.tv_order_from);
                TextView tvOrderTime = view.findViewById(R.id.tv_order_time);
                ImageView ivOrder = view.findViewById(R.id.iv_order);
                TextView tvStatus = view.findViewById(R.id.tv_status);
                if (helper.getItemViewType() == AppConstant.HOME_ORDER_UPDATE_TYPE) {
                    MsgView msgView = view.findViewById(R.id.msg_view);
                    final HomeOrderUpdateBean homeOrderUpdateBean = (HomeOrderUpdateBean) item;
                    tvOrderFrom.setText("来自" + homeOrderUpdateBean.getNickName() + "的订单");
                    tvOrderTime.setText(homeOrderUpdateBean.getCreateTime());
                    ImageLoaderUtils.displayCircle(mContext, ivOrder, homeOrderUpdateBean.getHeadPicUrl());
                    HomeOrderGoodsBean homeOrderGoodsBean = homeOrderUpdateBean.getOrderDetailList().get(0);
                    ImageLoaderUtils.displayRound(mContext, ivProductPic, homeOrderGoodsBean.getProductPicUrl(), 4);
                    tvSize.setText(String.format(mContext.getResources().getString(R.string.format_string_size), homeOrderGoodsBean.getColor(), homeOrderGoodsBean.getSize()));
                    tvProductName.setText(homeOrderGoodsBean.getProductName());
                    tvPrice.setText("￥" + homeOrderGoodsBean.getPrice());
                    tvSubmit.setText("查看详情");
                    tvSubmit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnOrderClickListener != null) {
                                mOnOrderClickListener.onOrderClick(homeOrderUpdateBean);
                            }
                        }
                    });
                    UnreadMsgUtils.show(msgView, homeOrderUpdateBean.getMsgNum());
                } else if (helper.getItemViewType() == AppConstant.HOME_ORDER_ROB_TYPE) {
                    final HomeOrderRobBean homeOrderRobBean = (HomeOrderRobBean) item;
                    tvOrderFrom.setText("来自" + homeOrderRobBean.getNickName() + "的订单");
                    tvOrderTime.setText(homeOrderRobBean.getCreateTime());
                    ImageLoaderUtils.displayCircle(mContext, ivOrder, homeOrderRobBean.getHeadPicUrl());
                    if (homeOrderRobBean.getOrderDetailList().size() != 0) {
                        HomeOrderGoodsBean homeOrderGoodsBean = homeOrderRobBean.getOrderDetailList().get(0);
                        ImageLoaderUtils.displayRound(mContext, ivProductPic, homeOrderGoodsBean.getProductPicUrl(), 4);
                        tvSize.setText(String.format(mContext.getResources().getString(R.string.format_string_size), homeOrderGoodsBean.getColor(), homeOrderGoodsBean.getSize()));
                        tvProductName.setText(homeOrderGoodsBean.getProductName());
                        tvPrice.setText("￥" + homeOrderGoodsBean.getPrice());
                    }
                    tvSubmit.setText("立即抢单");
                    tvSubmit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnRobOrderListener != null) {
                                mOnRobOrderListener.onRobOrder(homeOrderRobBean);
                            }
                        }
                    });
                }
                break;
            case AppConstant.HOME_MSG_TYPE:
                final HomeMsgBean homeMsgBean = (HomeMsgBean) item;
                ImageView ivAvatar = view.findViewById(R.id.iv_client_avatar);
                TextView tvUserName = view.findViewById(R.id.tv_client_name);
                TextView tvMsgTime = view.findViewById(R.id.tv_msg_time);
                TextView tvContent = view.findViewById(R.id.tv_content);
                MsgView msgView = view.findViewById(R.id.msg_view);

                NimUserInfo userInfo = (NimUserInfo) NimUIKit.getUserInfoProvider().getUserInfo(homeMsgBean.getAccount());
                if (userInfo != null) {
                    ImageLoaderUtils.displayCircle(mContext, ivAvatar, userInfo.getAvatar());
                }
                tvMsgTime.setText(TimeUtils.millis2String(homeMsgBean.getTime()));
                tvUserName.setText(homeMsgBean.getName());
                tvContent.setText(homeMsgBean.getMessage());
                UnreadMsgUtils.show(msgView, homeMsgBean.getCount());

                tvSubmit.setText("查看详情");
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnMessageClickListener != null) {
                            mOnMessageClickListener.onMessageClick(homeMsgBean);
                        }
                    }
                });
                break;
            case AppConstant.HOME_MIJI_TYPE:
                final HomeMijiBean homeMijiBean = (HomeMijiBean) item;
                tvMsgTime = view.findViewById(R.id.tv_msg_time);
                ImageView ivLogo = view.findViewById(R.id.iv_miji_logo);
                TextView tvMijiTitle = view.findViewById(R.id.tv_miji_title);
                TextView tvTutor = view.findViewById(R.id.tv_miji_tutor);
                tvMsgTime.setText(homeMijiBean.getCreateTime());
                ImageLoaderUtils.getGlideManager().asBitmap().load(((HomeMijiBean) item).getEsoCoverPicUrl() + "?imageView2/1/w/200/h/200/interlace/1/q/100")
                        .apply(ImageLoaderUtils.getSGCommonGlideOption(4)).into(ivLogo);
                tvMijiTitle.setText(((HomeMijiBean) item).getEsoTitle());
                tvTutor.setText("资深导师：" + ((HomeMijiBean) item).getTutorName());
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnMijiClickListener != null) {
                            mOnMijiClickListener.onMijiClick(homeMijiBean);
                        }
                    }
                });
                break;
            case AppConstant.HOME_SYSTEM_TYPE:
                final UpdateInfoBean updateInfoBean = (UpdateInfoBean) item;
                tvMsgTime = view.findViewById(R.id.tv_msg_time);
                tvMsgTime.setText(updateInfoBean.getReleaseTime());
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnSystemUpdateClickListener != null) {
                            mOnSystemUpdateClickListener.onUpdateClick(updateInfoBean);
                        }
                    }
                });
                break;
            case AppConstant.HOME_NEARSHOP_RECOMMEND:
                final HomeRecommendBean recommendBean = (HomeRecommendBean) item;
                ImageView ivActivityBack = view.findViewById(R.id.iv_activity_back);
                tvMsgTime = view.findViewById(R.id.tv_msg_time);
                tvMsgTime.setText(recommendBean.getCreateTime());
                ImageLoaderUtils.getGlideManager().asBitmap().load(((HomeRecommendBean) item).getMsgCoverPicUrl() + "?imageView2/1/w/600/h/200/interlace/1/q/100")
                        .apply(ImageLoaderUtils.getSGCommonGlideOption(4)).into(ivActivityBack);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnRecommendClickListener != null) {
                            mOnRecommendClickListener.onRecommendClick(recommendBean);
                        }
                    }
                });
                break;
        }
    }

    @Override
    public Paint dividerPaint(int position, RecyclerView parent) {
        Paint paint = new Paint();
        paint.setColor(mContext.getResources().getColor(R.color.sg_common_line_bg_dark));
        paint.setStrokeWidth(20);
        return paint;
    }

    public interface OnOrderClickListener {
        void onOrderClick(HomeOrderUpdateBean bean);
    }

    private OnOrderClickListener mOnOrderClickListener;

    public void setOnOrderClickListener(OnOrderClickListener listener) {
        mOnOrderClickListener = listener;
    }

    public interface OnRobOrderListener {
        void onRobOrder(HomeOrderRobBean bean);
    }

    private OnRobOrderListener mOnRobOrderListener;

    public void setOnRobOrderListener(OnRobOrderListener listener) {
        mOnRobOrderListener = listener;
    }

    public interface OnMessageClickListener {
        void onMessageClick(HomeMsgBean msgBean);
    }

    private OnMessageClickListener mOnMessageClickListener;

    public void setOnMessageClickListener(OnMessageClickListener listener) {
        mOnMessageClickListener = listener;
    }

    public interface OnMijiClickListener {
        void onMijiClick(HomeMijiBean mijiBean);
    }

    private OnMijiClickListener mOnMijiClickListener;

    public void setOnMijiClickListener(OnMijiClickListener listener) {
        mOnMijiClickListener = listener;
    }

    public interface OnSystemUpdateClickListener {
        void onUpdateClick(UpdateInfoBean updateInfoBean);
    }

    private OnSystemUpdateClickListener mOnSystemUpdateClickListener;

    public void setOnSystemUpdateClickListener(OnSystemUpdateClickListener listener) {
        mOnSystemUpdateClickListener = listener;
    }

    public interface OnRecommendClickListener {
        void onRecommendClick(HomeRecommendBean bean);
    }

    private OnRecommendClickListener mOnRecommendClickListener;

    public void setOnRecommendClickListener(OnRecommendClickListener listener) {
        mOnRecommendClickListener = listener;
    }
}
